<template>
<div :style="height">
  <div class="main_style" >
    <img class="top_button" :src="getVoiceImg()" @click="showBackgroundVoice" alt="">
    <el-row class="button_group">
      <el-button @click="register()"><h4><span>新</span>
        <span>用</span>
        <span>户</span></h4></el-button>
      <el-button type="primary" @click="showLogin()"><h4><span>登</span>
        <span>录</span>
        <span>游</span>
        <span>戏</span></h4></el-button>
      <el-button type="success"><h4><span>关</span>
        <span>于</span>
        <span>我</span>
        <span>们</span></h4></el-button>
    </el-row>
  </div>
<!--登录弹窗-->
  <div v-if="login_flag" class="loginBox" >
    <div class="title1">
      <h2>登录</h2>
      <i class="el-icon-close" @click="closeLogin()"></i>
    </div>
      <el-form :model="loginInfo"   label-width="100px">
        <div class="item">
          <input type="text" v-model="loginInfo.account" autocomplete="off"></input>
          <label>account</label>
        </div>
          <div class="item">
          <input type="password" v-model="loginInfo.password" autocomplete="new-password"></input>
          <label>password</label>
          </div>
        <el-form-item>
          <button  @click.prevent="login()" class="btn">进入游戏</button>
          <button @click="closeLogin()" class="btn">取消</button>
        </el-form-item>
      </el-form>
  </div>
</div>
</template>

<script>
import Vue from "vue";
export default {
  name: "GameInit",
  data(){
    return{
      backMusic: null,
      backOgg: null,
      musicStop: 0,
      login_flag:false,
      register_flag:false,
      height: {
        height: ""
      },
      loginInfo:{
        account:'',
        password:''
      },
      all_users:[],
      user_info:{
        account:'',
        password:'',
        monkeyKing:''
      }
    };
  },
  methods:{
    InitHeight() {
      this.height.height = window.innerHeight + "px";
    },
    register(){
      console.log("注册")
      this.$router.push('/Register');
    },
    showLogin(){
      this.login_flag = !this.login_flag;
      const bg = document.getElementsByClassName("main_style");
      bg[0].style.opacity = 0.5
    },
    getVoiceImg(){
      while(this.musicStop === 0){
        return require(`../images/startMusic.png`)
      }
      while(this.musicStop === 1){
        return require(`../images/stop.png`)
      }
    },
    showBackgroundVoice(){
      if(this.musicStop===0){
        this.backOgg.play()
        this.musicStop = 1
      }else{
        this.backOgg.pause()
        this.musicStop = 0
      }
    },
    // playMusic(){
    //   this.$data.backMusic = "../static/backgroundMusic.ogg"
    //   this.$data.backOgg = new Audio(this.backMusic)
    //   this.$data.backOgg.play()
    //
    // },
    login(){
      if(this.isValid()){
        this.backOgg.pause()
        this.backMusic = "../static/load.mp3"
        this.backOgg = new Audio(this.backMusic)
        //this.backOgg.play()
         this.$router.push({
            path:"/GameLoading",
            query:{
            destination:'GameStart'
          }
        })
      }
      else{
        alert("登录失败")
      }
    },
    isValid(){
      for(let i=0;i<this.$data.all_users.length;i++){
        let user = JSON.parse(this.$data.all_users[i])
        if(user.account===this.$data.loginInfo.account&&user.password===this.$data.loginInfo.password){
          Vue.prototype.$user_account = user.account;
          Vue.prototype.$user = i;
          Vue.prototype.$user_password = user.password;
          Vue.prototype.$last_king = user.monkeyKing;
          console.log(this.$user_account+"   "+this.$user)
          return true
        }
      }
      return false
    },
    closeLogin(){
      this.login_flag = !this.login_flag;
      this.$message("取消登录");
      const bg = document.getElementsByClassName("main_style");
      bg[0].style.opacity = 1
    }
  },
  created() {
    this.InitHeight();
   // localStorage.clear()
    this.backMusic = "../static/login.mp3"
    this.backOgg = new Audio(this.backMusic)
    this.$data.all_users = JSON.parse(localStorage.getItem("all_users"))
    console.log("初始化"+this.$data.all_users)
  },
}
</script>

<style scoped>
@import url('http://fonts.googleapis.com/css?family=Poppins:100,200,300,400,500,600,700,800,900');
* {
  margin: 0;
  padding: 0;
  font-family:  "Lucida Console", "Courier New", monospace;
}
.main_style{
  width: 100%;
  height: 100%;
  position: relative;
  background-image:url('../assets/monkeys/bgpm.jpg');
  z-index: 1;
}
.top_button {
  border: 0;
  position: absolute;
  right: 10px;
  top: 10px;
  width: 80px;
  height: 80px;
}
.top_button:hover{
  border-radius: 5px;
  color: #fff;
  background: #fd9001;
  border-radius: 1px;
  box-shadow: 0 0 5px 0 #03e9f4,
  0 0 25px 0 #03e9f4,
  0 0 50px 0 #03e9f4,
  0 0 100px 0 #03e9f4;
  transition: all .5s linear;
}


.button_group{
  position: absolute;
  bottom: 30px;
  left: 500px;
}
.el-button{
background-color: transparent;border:0;
  margin: 10px;
}
h4:hover span {
  margin-right: 0.7px;
}
h4 {
  color: #fd4c00;
  font-size: 2em;
  font-weight: 500;
  letter-spacing: 1px;
  cursor: pointer;
  text-shadow: 2px 2px 5px blue;
  font-family: "Lucida Console", "Courier New", monospace;
}

h4 span {
  transition: .5s;
}
h4:hover span {
  color: #fff;
  text-shadow: 0 0 10px #00f,
  0 0 20px #00f,
  0 0 30px #00f,
  0 0 80px #00f,
  0 0 120px #00f;
}

.loginBox {
  position: absolute;
  top: 150px;
  left: 600px;
  width: 400px;
  height: 364px;
  background-color: #0c1622;
  margin: 100px auto;
  border-radius: 10px;
  box-shadow: 0 15px 25px 0 rgba(0, 0, 0, .6);
  padding: 40px;
  box-sizing: border-box;
  z-index: 10;
}

.loginBox h2 {
  text-align: center;
  color: aliceblue;
  margin-bottom: 30px;
  font-family: 'Courier New', Courier, monospace;
}

.loginBox button,
.loginBox input{
  background: transparent;
  border: 0;
  outline: none;
}
.loginBox .item {
  height: 45px;
  border-bottom: 1px solid #fff;
  margin-bottom: 40px;
  position: relative;
  color: #03e9f4;
}

.item input {
  width: 100%;
  height: 100%;
  color: #fff;
  padding-top: 20px;
  box-sizing: border-box;
}
.item label {
  position: absolute;
  left: 0;
  top: 9px;
  transition: all 0.5s linear;
}
.item input:focus+label{
  top: 0px;
  font-size: 2px;
}

.btn {
  padding: 10px 20px;
  margin-top: 30px;
  color: #03e9f4;
  position: relative;
  overflow: hidden;
  text-transform: uppercase;
  letter-spacing: 2px;
  left: 25%;
}

.btn:hover {
  border-radius: 5px;
  color: #fff;
  background: #03e9f4;
  box-shadow: 0 0 5px 0 #03e9f4,
  0 0 25px 0 #03e9f4,
  0 0 50px 0 #03e9f4,
  0 0 100px 0 #03e9f4;
  transition: all 1s linear;
}
</style>
